<template>
  <div class="img-list">
    <my-header titleName="图文资讯"></my-header>
    <div class="cate">
      <ul id="cateul">
        <li @click="changeImg(0)"><span>全部</span></li>
        <li  v-for="item in ImgCate" :key="item.id" @click="changeImg(item.id)"><span>{{item.title}}</span></li>
      </ul>
    </div>
    <div class="img-list-box">
      <ul>
        <li v-for="item in imgList" :key="item.id">
          <router-link :to="`/detail/${item.id}`">
            <img :src="item.img_url" :alt="item.title">
            </router-link>
          <p><span class="title">{{item.title}}</span>{{item.zhaiyao}}</p>
        </li>
      </ul>
    </div>
  </div>
</template>
<script>
import { getImgCate, getImgList } from '@/api'
import myHeader from '@/components/myHeader'
export default {
  components: {
    myHeader
  },
  data () {
    return {
      ImgCate: [],
      imgList: []
    }
  },
  created () {
    this.initImgCate()
    this.initImgList(0)
  },
  methods: {
    initImgCate () {
      getImgCate()
        .then(res => {
          console.log(res)
          this.ImgCate = res.message
          let usernum = this.ImgCate.length + 1
          let w = usernum * 60 + 'px'
          document.getElementById('cateul').style.width = w
        })
    },
    initImgList (id) {
      getImgList(id)
        .then(res => {
          console.log(res)
          this.imgList = res.message
        })
    },
    changeImg (id) {
      this.initImgList(id)
    }
  }
}
</script>

<style lang="less" scoped>
.img-list {
    margin-bottom: 55px;
    margin-top: 45px;
  }
  .cate {
    max-width: 100%;
    margin: 0;
    overflow-x:auto;
    overflow-y: hidden;
    font-size: 14px;
    ul {
      padding-left: 10px;
      margin: 5px 0;
      li {
        list-style: none;
        display: inline-block;
        padding: 0 0 0 5px;
        span {
          color:#0094ff;
        }
      }
    }
  }
  .img-list-box {
    ul {
      padding: 0;
      li {
        list-style: none;
        position: relative;
      }
    }
    p {
      font-size: 14px;
      color: #fff;
      position: absolute;
      bottom: 0px;
      left: 0px;
      background-color: rgba(0,0,0,0.4);
      .title {
        font-weight: bold;
      }
    }
    img {
      width: 100%;
    }
  }

  image[lazy=loading] {
    width: 100%;
    margin: auto;
  }
</style>
